# Maintainer: Jelle van der Waa <jelle@archlinux.org>
# Maintainer: Morten Linderud <foxboron@archlinux.org>

pkgname=step-cli
pkgver=0.24.3
pkgrel=1
pkgdesc="A zero trust swiss army knife for working with X509, OAuth, JWT, OATH OTP, etc."
url="https://github.com/smallstep/cli"
arch=(loong64 x86_64)
license=(Apache)
depends=(glibc)
makedepends=(go)
optdepends=("step-ca: setup CA/SSO/ACME server")
source=($pkgname-$pkgver.tar.gz::https://github.com/smallstep/cli/archive/refs/tags/v${pkgver}.tar.gz)
sha512sums=('6fe199752d9020446106ad91696c69335d137ad7abaa65c8dabaac6ac48cea0f70274a84f87e34b22d7283fd2e61a635010936dbd638f28d76f69c90b6f6c0c2')

prepare() {
  cd cli-${pkgver}
  sed -i "s/step/${pkgname}/g" "autocomplete/zsh_autocomplete"
  sed -i "s/step/${pkgname}/g" "autocomplete/bash_autocomplete"
}

build() {
  export BUILD_DATE="$(date --utc --date="@${SOURCE_DATE_EPOCH:-$(date +%s)}" +%Y-%m-%d)"

  cd cli-${pkgver}
  go build \
      -trimpath \
      -buildmode=pie \
      -mod=readonly \
      -modcacherw \
      -ldflags "-linkmode external -extldflags \"${LDFLAGS}\" -X \"main.Version=${pkgver}\" -X \"main.BuildTime=${BUILD_DATE}\"" \
      -o step-cli cmd/step/main.go
}

check() {
  cd cli-${pkgver}
  go test ./...
}

package() {
  cd cli-${pkgver}
  install -Dm755 $pkgname "$pkgdir"/usr/bin/$pkgname
  install -Dm644 "autocomplete/bash_autocomplete" "$pkgdir/usr/share/bash-completion/completions/$pkgname"
  install -Dm644 "autocomplete/zsh_autocomplete" "$pkgdir/usr/share/zsh/site-functions/_${pkgname}"
}
